package ipdesk.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginSessionCheckInterceptor extends AbstractInterceptor
{
	public String intercept(ActionInvocation invocation) throws Exception
	{
		/*
		 * 액션 수행전 처리할 작업
		 * 1. userEmail 세션이 존재하는지 체크.
		 * 2. 존재하면 invocation.invokr() 호출.
		 * 3. 존재하지 않으면 로그인 페이지로 리다이렉트.
		 */
		System.out.println("액션 수행전");
		System.out.println(isLogined());
		if(this.isLogined())
		{			
			return  invocation.invoke();
		}
		else
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.sendRedirect("/");
			
			return null;
		}
		
	}
	
	private boolean isLogined()
	{
		ActionContext context = ActionContext.getContext(); //session을 생성하기 위해
		Map<String, String> session = (Map<String, String>)context.getSession();// Map 사용시
		context.setSession(session);
		
		if(session.get("userEmail") == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
